# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=mathgl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.0.1
pkgrel=4
pkgdesc="Library for high-quality scientific graphics (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://mathgl.sourceforge.io/"
license=('spdx:GPL-3.0-or-later AND LGPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-fltk"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-zlib")
         #"${MINGW_PACKAGE_PREFIX}-wxWidgets"
source=("${_realname}-${pkgver}.tar.gz::https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz"
        "001-fix-cmake-files.patch")
sha256sums=('ca84bf9480c39ed3112e920c16ce3d8f2898698f62896f3a57714128622a55aa'
            '27c6d40f04e3a10d40490c42fac6560a9f304bfbdba45614e7de6f830a8bb820')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/001-fix-cmake-files.patch"
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  CXXFLAGS=" -Wno-deprecated-declarations" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -Denable-all-widgets=OFF \
    -Denable-double=ON \
    -Denable-fltk=ON \
    -Denable-gif=ON \
    -Denable-glut=ON \
    -Denable-gsl=ON \
    -Denable-hdf5=ON \
    -Denable-jpeg=ON \
    -Denable-mgl2=ON \
    -Denable-opengl=ON \
    -Denable-png=ON \
    -Denable-qt5=ON \
    -Denable-openmp=OFF \
    -DGLUT_glut_LIBRARY_RELEASE=${MINGW_PREFIX}/lib/libfreeglut.dll.a \
    "../${_realname}-${pkgver}"

    # -Denable-pdf=ON
    # -Denable-wx=ON

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  # fixup references in associated cmake modules
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _ff in ${pkgdir}${MINGW_PREFIX}/lib/cmake/mathgl2/*.cmake; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}
